[Music]
Welcome to Asynchronous I/O with Callback
Programming. After watching this video, you should be able
to: Explain the concept of asynchronous callback
functions and handle inbound hypertext transfer protocol
(HTTP) method calls for a server resource. Network operations run in an asynchronous
manner. For example, the response from a web service
call might not return immediately. When an application blocks (or waits) for
a network operation to complete, that application wastes processing time on the server. Node.js makes all network operations in a
non-blocking manner. Every network operation returns immediately. To handle the result from a network call,
write a callback function that Node.js calls when the network operation completes. This sequence diagram for a scenario shows
the interaction between the application, the Node.js framework, the web service call to
the remote server, and the call back to the callback function. In the first step, the application makes a
call to HTTP.request. This function makes a call to the remote web
server and requests the web service. Before the Node.js framework receives the
HTTP response message from the remote web server, it immediately returns a result for
the HTTP.request function call. This result simply indicates that the request message
was sent successfully. It does not say anything about the response
message. When the Node.js framework receives an HTTP
response message from the remote server, it calls the callback function that you defined
during the HTTP.request function call. This function handles the HTTP response message. In a slightly more complex scenario, your
application calls a custom Node.js module, which then makes an HTTP.request function
call. The Node.js framework then calls the remote
server's web service by sending an HTTP request message. In the same manner as in the first scenario,
the Node.js framework returns a value to the HTTP function call in the Node.js module. This response simply states that the HTTP
request was successfully sent out. The Node.js module then returns from the exported
function call. At this point, the application continues processing
on to the next step, while the response message has not yet been sent. When the remote server returns an HTTP response
message, the Node.js framework calls the callback function defined by the custom Node.js module. The purpose of the callback function is to
handle two events: request.on('data') and request.on('end'). In this case, the callback function simply
prints the HTTP response message body to the console log. This code example shows you how to make an
HTTP request call from a function inside a Node.js module. The first parameter in the HTTP request function
is an options variable. The options variable included at least two
variables: the hostname of the remote server, and a uniform resource locator (URL) resource
path that you want to act upon. In the example here, you are making a call
to the US National Weather Service to retrieve the weather observation from San Francisco
International Airport (KSFO). The second parameter of the HTTP request function
is a callback function. In this case, it is an anonymous function
that receives one parameter: the response object. When the Node.js module calls this anonymous
function, events occur while it is receiving parts of the HTTP response object. In this example, there are two specific events:
a 'data' event and an 'end' event. For these two events, you define more callback
functions to handle each event type. In the actual coding, you may need to use
HTTPS instead of HTTP. The result object is passed into the callback
function of a parseString module. The HTTP.request function takes in a URL and
a set of options. If both are passed, the two are merged, with
options taking precedence. You can define the host, ports, authentication,
protocol, and other headers in the options object. The HTTP.request method also accepts an optional
callback function that is invoked immediately once a response is received. When HTTP.request calls the callback function,
it passes a response object in the first parameter of the callback function. This callback function has the response object
as the first parameter. The Node.js framework emits several events
during the course of the request function. You can listen to these events by using the
object.on() method and passing in the event name as the first parameter. If the request is successful, a 'data' event
is emitted on the response object every time data comes in, followed by an 'end' event
when the response finishes. If the request fails, there is an 'error'
event followed by the 'close' event. Let's see how to handle such errors. The request method returns an object of type
HTTP.ClientRequest. This object represents the request in progress. You can append to the request body, make changes
to the headers, and listen for error events as shown here. The code simply outputs the
error message if there is an error. To end the request, call clientRequest.end(). In this video, you learned that: When an application blocks for a network operation
to complete, that application wastes processing time on the server. Node.js makes all network operations in a
non-blocking manner. and when the Node.js framework receives an HTTP
response message from the remote server, it calls the callback function that handles the
HTTP response message.